package com.jk.myimkit.imkit.fragment;

import android.os.Message;
import android.text.TextUtils;
import android.view.View;

import com.jk.myimkit.R;
import com.jk.myimkit.imkit.RongContext;
import com.jk.myimkit.imkit.RongIM;
import com.jk.myimkit.imkit.model.Event;

import io.rong.common.RLog;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;

public class SetConversationNotificationFragment extends BaseSettingFragment {
    private static final String TAG = "SetConversationNotificationFragment";

    public SetConversationNotificationFragment() {
    }

    public static SetConversationNotificationFragment newInstance() {
        return new SetConversationNotificationFragment();
    }

    protected void initData() {
        if (RongContext.getInstance() != null) {
            RongContext.getInstance().getEventBus().register(this);
        }

        RongIM.getInstance().getConversationNotificationStatus(this.getConversationType(), this.getTargetId(), new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {
            public void onSuccess(Conversation.ConversationNotificationStatus notificationStatus) {
                if (notificationStatus != null) {
                    SetConversationNotificationFragment.this.setSwitchBtnStatus(notificationStatus != Conversation.ConversationNotificationStatus.DO_NOT_DISTURB);
                }

            }

            public void onError(RongIMClient.ErrorCode errorCode) {
                SetConversationNotificationFragment.this.setSwitchBtnStatus(!SetConversationNotificationFragment.this.getSwitchBtnStatus());
            }
        });
    }

    protected boolean setSwitchButtonEnabled() {
        return true;
    }

    protected String setTitle() {
        return this.getString(R.string.rc_setting_conversation_notify);
    }

    public boolean handleMessage(Message msg) {
        return false;
    }

    protected void onSettingItemClick(View v) {
        RLog.i("SetConversationNotificationFragment", "onSettingItemClick, " + v.toString());
    }

    protected int setSwitchBtnVisibility() {
        return 0;
    }

    protected void toggleSwitch(boolean toggle) {
        Conversation.ConversationNotificationStatus status;
        if (toggle) {
            status = Conversation.ConversationNotificationStatus.NOTIFY;
        } else {
            status = Conversation.ConversationNotificationStatus.DO_NOT_DISTURB;
        }

        if (this.getConversationType() != null && !TextUtils.isEmpty(this.getTargetId())) {
            RongIM.getInstance().setConversationNotificationStatus(this.getConversationType(), this.getTargetId(), status, new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {
                public void onSuccess(Conversation.ConversationNotificationStatus status) {
                    RLog.i("SetConversationNotificationFragment", "SetConversationNotificationFragment onSuccess--");
                }

                public void onError(RongIMClient.ErrorCode errorCode) {
                    SetConversationNotificationFragment.this.setSwitchBtnStatus(!SetConversationNotificationFragment.this.getSwitchBtnStatus());
                }
            });
        } else {
            RLog.e("SetConversationNotificationFragment", "SetConversationNotificationFragment Arguments is null");
        }

    }

    public void onEventMainThread(Event.ConversationNotificationEvent event) {
        if (event != null && event.getTargetId().equals(this.getTargetId()) && event.getConversationType().getValue() == this.getConversationType().getValue()) {
            this.setSwitchBtnStatus(event.getStatus() == Conversation.ConversationNotificationStatus.NOTIFY);
        }

    }

    public void onDestroy() {
        if (RongContext.getInstance() != null) {
            RongContext.getInstance().getEventBus().unregister(this);
        }

        super.onDestroy();
    }
}
